/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on 2009. 1. 21, 오후 6:12:26
 */

package com.jay.gui;


import com.jay.database.DBInfo;
import com.jay.database.DatabaseConnection;
import com.jay.util.CommonConst;
import com.jay.util.CommonUtil;
import com.jay.util.FileHandler;
import com.jay.util.JayCipher;
import com.jay.util.JayException;
import java.awt.Frame;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


/**
 *
 * @author jay
 */
public class ConnectDialogBean extends javax.swing.JDialog {

    Properties mProp = null;
    Object mOwner;
    JayFrame jf;
    JayCipher jc = null;
    
    HashMap mConnections = null;
    HashMap mCurrConn = null;
    HashMap encCurrConn = new HashMap();
    HashMap encConns = new HashMap();
    ExceptionDialog ed = null;
    ButtonGroup bg = null;
    private boolean mModfFlag = false;
    private String mSelectedKey = null;
    CommonUtil cu = new CommonUtil();
    
    HashMap hiddenColumns = new HashMap();
    TableColumnModel mTcm = null;
    
    /** Creates new form NewJFrame */
    public ConnectDialogBean(Frame aOwner){
        mOwner = aOwner;
        initComponents();
        initialize();
        ed = new ExceptionDialog(aOwner);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        NewButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        DBConnectionInfoPannel = new javax.swing.JPanel();
        DatabaseTypeLabel = new javax.swing.JLabel();
        DatabaseTypeCombo = new javax.swing.JComboBox();
        HostIPLabel = new javax.swing.JLabel();
        PortLabel = new javax.swing.JLabel();
        DBNameLabel = new javax.swing.JLabel();
        UserLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        SaveButton = new javax.swing.JButton();
        HostIPField = new javax.swing.JTextField();
        PortField = new javax.swing.JTextField();
        DBNameField = new javax.swing.JTextField();
        UserField = new javax.swing.JTextField();
        PasswordField = new javax.swing.JPasswordField();
        TestButton = new javax.swing.JButton();
        InformLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        LabelField = new javax.swing.JTextField();
        ConnectButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        Export = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Connect Dialog");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        NewButton.setText("New");
        NewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NewButtonActionPerformed(evt);
            }
        });

        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        DBConnectionInfoPannel.setPreferredSize(new java.awt.Dimension(250, 300));

        DatabaseTypeLabel.setText("Database :");

        DatabaseTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        HostIPLabel.setText("Host IP :");

        PortLabel.setText("Port :");

        DBNameLabel.setText("DBName(SID) :");

        UserLabel.setText("User :");

        PasswordLabel.setText("Password :");

        SaveButton.setText("Save");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        HostIPField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                HostIPFieldFocusGained(evt);
            }
        });

        PortField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                PortFieldFocusGained(evt);
            }
        });

        DBNameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                DBNameFieldFocusGained(evt);
            }
        });

        UserField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                UserFieldFocusGained(evt);
            }
        });

        PasswordField.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        PasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                PasswordFieldFocusGained(evt);
            }
        });

        TestButton.setText("Test");
        TestButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TestButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Label :");

        LabelField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LabelFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout DBConnectionInfoPannelLayout = new javax.swing.GroupLayout(DBConnectionInfoPannel);
        DBConnectionInfoPannel.setLayout(DBConnectionInfoPannelLayout);
        DBConnectionInfoPannelLayout.setHorizontalGroup(
            DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DBConnectionInfoPannelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(DBConnectionInfoPannelLayout.createSequentialGroup()
                        .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(DatabaseTypeLabel)
                            .addComponent(jLabel1))
                        .addGap(38, 38, 38)
                        .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(DatabaseTypeCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(LabelField)))
                    .addGroup(DBConnectionInfoPannelLayout.createSequentialGroup()
                        .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PasswordLabel)
                            .addComponent(UserLabel)
                            .addComponent(DBNameLabel)
                            .addComponent(PortLabel)
                            .addComponent(HostIPLabel))
                        .addGap(12, 12, 12)
                        .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(HostIPField)
                            .addComponent(PortField)
                            .addComponent(DBNameField)
                            .addComponent(UserField)
                            .addComponent(PasswordField)))
                    .addGroup(DBConnectionInfoPannelLayout.createSequentialGroup()
                        .addComponent(TestButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(SaveButton))
                    .addComponent(InformLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        DBConnectionInfoPannelLayout.setVerticalGroup(
            DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(DBConnectionInfoPannelLayout.createSequentialGroup()
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(LabelField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DatabaseTypeLabel)
                    .addComponent(DatabaseTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HostIPLabel)
                    .addComponent(HostIPField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PortLabel)
                    .addComponent(PortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DBNameLabel)
                    .addComponent(DBNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UserLabel)
                    .addComponent(UserField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordLabel)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(DBConnectionInfoPannelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TestButton)
                    .addComponent(SaveButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(InformLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        ConnectButton.setText("Connect");
        ConnectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConnectButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        Export.setText("Export");
        Export.setName("Export"); // NOI18N
        Export.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExportActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(9, 9, 9))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(NewButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Export)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(DeleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DBConnectionInfoPannel, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(ConnectButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                        .addComponent(CancelButton)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DBConnectionInfoPannel, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(NewButton)
                            .addComponent(DeleteButton)
                            .addComponent(ConnectButton)
                            .addComponent(CancelButton)
                            .addComponent(Export))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void initialize(){
        ImageIcon icon = new ImageIcon(CommonConst.ARARE_PNG_PATH);
        this.setIconImage(icon.getImage());
        jc = new JayCipher();
        mCurrConn = new HashMap();
        mConnections = readConnsInfo();
        mTcm = this.jTable1.getColumnModel();
        if(mConnections == null) mConnections = new HashMap();
        else setConnsTable();
        this.setDataBaseType();
//        bg = new ButtonGroup();
//        bg.add(this.jRadioButton1);
//        bg.add(this.jRadioButton2);        
        this.setFocusableWindowState(true);
        sortTable();
    }

    public void hideColumn(int iColIdx) {
        TableColumn column = mTcm.getColumn(iColIdx);
        hiddenColumns.put(iColIdx, column);
        jTable1.removeColumn(column);
    }

    public void showColumn(int iColIdx) {
        Object o = hiddenColumns.remove(iColIdx);
        if (o == null) {
            return;
        }
        mTcm.addColumn((TableColumn) o);
        int lastColumn = mTcm.getColumnCount() - 1;
        if (iColIdx < lastColumn) {
            mTcm.moveColumn(lastColumn, iColIdx);
        }
    }
    
    private boolean chkConnInfo(){
        boolean ret = false;
        if(this.LabelField.getText().length()>0 &&
                HostIPField.getText().length()>0 &&
                PortField.getText().length()>0 &&
                DBNameField.getText().length()>0 &&
                UserField.getText().length()>0 ){
             ret = true;
             
        }
        return ret;
    }

    private void makeCurrConn() {
        mCurrConn.put(CommonConst.LABEL_STRING, this.LabelField.getText());
        mCurrConn.put(CommonConst.DB_TYPE_STRING, this.DatabaseTypeCombo.getSelectedItem());
        mCurrConn.put(CommonConst.HOST_STRING, HostIPField.getText());
        mCurrConn.put(CommonConst.PORT_STRING, PortField.getText());
        mCurrConn.put(CommonConst.SID_STRING, DBNameField.getText());
        mCurrConn.put(CommonConst.USER_STRING, UserField.getText());
        mCurrConn.put(CommonConst.PASSWORD_STRING, new String(PasswordField.getPassword()));
        
        StringBuilder aTemp = new StringBuilder();
        for(int i=0;i<8;i++){
            aTemp.append(CommonConst.ASTERISK);
        }
        mCurrConn.put(CommonConst.PASSWORD_ENC_STRING, aTemp.toString()); 
        
//        Enumeration<AbstractButton> enums = bg.getElements();
//        JRadioButton jb = null;
//        while(enums.hasMoreElements()){ //hasMoreElements() Enum에 더 꺼낼 개체가 있는지
//            jb = (JRadioButton)(enums.nextElement());
//            if(jb.isSelected()){
//                mCurrConn.put(CommonConst.TERM_TYPE, jb.getName().trim());
//                break;
//            }
//        }
//        System.out.println(mCurrConn);
    }

    /**
     * @param 
     * @param 
     * @return
     */
    private void setConnsTable(){
        try {
            Vector colNameVector = new Vector();
            for(int i=0;i<CommonConst.CONNECT_CONFIG_COLUMN.length-1;i++){
                colNameVector.addElement(CommonConst.CONNECT_CONFIG_COLUMN[i]);
            }     
            Vector aTempVector = toVector(mConnections);
            DefaultTableModel tableModel;
            tableModel = new DefaultTableModel(aTempVector, colNameVector){
                @Override
                public boolean isCellEditable(int row, int column){
                    return false;//This causes all cells to be not editable
                }
            };
            
            jTable1.getTableHeader().setReorderingAllowed(false);
            jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            
//            jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            this.jTable1.setShowVerticalLines(true);
            this.jTable1.getTableHeader().setReorderingAllowed(false);
            
            jTable1.setModel(tableModel);
            jTable1.getColumnModel().getColumn(3).setPreferredWidth(100);
            jTable1.getColumnModel().getColumn(4).setPreferredWidth(50);
            jTable1.getColumnModel().getColumn(5).setPreferredWidth(100);
            hideColumn(0);
            hideColumn(jTable1.getColumnModel().getColumnCount()-1);
        } catch (Exception e) {
            ed.viewExceptionDialog(e);
        }
    }
    
    private void setDataBaseType(){
        DefaultComboBoxModel mComboModel = new DefaultComboBoxModel(CommonConst.DATABASE_TYPE);
        DatabaseTypeCombo.setModel(mComboModel);
    }
    
    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        // TODO add your handling code here:
        saveConnsInfo();
}//GEN-LAST:event_SaveButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        this.dispose();
        ((JayFrame)mOwner).setVisible(true);
}//GEN-LAST:event_CancelButtonActionPerformed

    private void ConnectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConnectButtonActionPerformed
        sendConnectionInfo();
}//GEN-LAST:event_ConnectButtonActionPerformed

    private void NewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewButtonActionPerformed
        // TODO add your handling code here:
        setInitConnsInfo();
    }//GEN-LAST:event_NewButtonActionPerformed

    private void HostIPFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_HostIPFieldFocusGained
        // TODO add your handling code here:
        this.HostIPField.selectAll();
    }//GEN-LAST:event_HostIPFieldFocusGained

    private void PortFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PortFieldFocusGained
        // TODO add your handling code here:
        this.PortField.selectAll();
    }//GEN-LAST:event_PortFieldFocusGained

    private void DBNameFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_DBNameFieldFocusGained
        // TODO add your handling code here:
        this.DBNameField.selectAll();
    }//GEN-LAST:event_DBNameFieldFocusGained

    private void UserFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_UserFieldFocusGained
        // TODO add your handling code here:
        this.UserField.selectAll();
    }//GEN-LAST:event_UserFieldFocusGained

    private void PasswordFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PasswordFieldFocusGained
        // TODO add your handling code here:
        this.PasswordField.selectAll();
    }//GEN-LAST:event_PasswordFieldFocusGained
     
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        for(int i=0;i<this.DatabaseTypeCombo.getItemCount();i++){
            if(this.DatabaseTypeCombo.getItemAt(i).equals((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 2)))){
                this.DatabaseTypeCombo.setSelectedIndex(i);
                break;
            }
        }

        this.LabelField.setText((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 1)));
        this.HostIPField.setText((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 3)));
        this.PortField.setText((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 4)));
        this.DBNameField.setText((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 5)));
        this.UserField.setText((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 6)));            
        this.PasswordField.setText((String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 7)));

        mSelectedKey = (String)(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0));
        mModfFlag = true;

        this.TestButton.setEnabled(true);
        this.InformLabel.setText(null);

        if(evt.getClickCount()==2) this.sendConnectionInfo();           
    }//GEN-LAST:event_jTable1MouseClicked

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        // TODO add your handling code here:
        int iSelectedRowNum = jTable1.getSelectedRow();
        if(iSelectedRowNum == -1){
            ed.viewExceptionDialog(new Exception(CommonConst.NO_DATA_SELECTED));
            return;
        }
        String sDeletedRowKey = (String)(jTable1.getModel().getValueAt(iSelectedRowNum, 0));
        ((DefaultTableModel)jTable1.getModel()).removeRow(iSelectedRowNum);
        this.mConnections.remove(sDeletedRowKey);
        refreshConnsInfo();   
    }//GEN-LAST:event_DeleteButtonActionPerformed

    private void TestButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TestButtonActionPerformed
        try {
            if(this.checkJDBCConnection()){
                this.InformLabel.setText(CommonConst.SUCCESS);
                this.TestButton.setEnabled(false);
            }else
                this.InformLabel.setText(CommonConst.FAIL);
        } catch (Exception ex) {
            ed.viewExceptionDialog(ex);
        }
    }//GEN-LAST:event_TestButtonActionPerformed

    private void LabelFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LabelFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_LabelFieldActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        if(this.mCurrConn.isEmpty())System.exit(0);
    }//GEN-LAST:event_formWindowClosed

    private void ExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExportActionPerformed
        // TODO add your handling code here:
        exportToXML();
    }//GEN-LAST:event_ExportActionPerformed

    private void exportToXML() {
        try {
            if(cu.export2File(mConnections, CommonConst.XML_STRING, this, null)){
                JOptionPane.showMessageDialog(this, "Infomation: " + CommonConst.SUCCESS_SAVE, "Information", JOptionPane.INFORMATION_MESSAGE);                
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ed.viewExceptionDialog(ex);
        }
    }
    
    private void sendConnectionInfo(){
        try {
            if(!chkConnInfo()) {
                throw new Exception(CommonConst.FAIL+CommonConst.NEW_LINE_N+"No information to connect database");
            }else{
                makeCurrConn();
                if(JayFrame.init(mCurrConn, mOwner)){
                    this.setVisible(false);
                    this.dispose();
                    ((JayFrame)mOwner).info(CommonConst.CONNECTED);   
                    ((JayFrame)mOwner).setVisible(true);
                    ((JayFrame)mOwner).getEditBean().getJEditorPane().setFocusable(true);
                    ((JayFrame)mOwner).getEditBean().getJEditorPane().setCaretPosition(0);                      
                }
            }
        } catch (Exception e1) {
            ed.viewExceptionDialog(e1);
        }
    }
    
    private HashMap cipherConnsInfo(HashMap srcConnsMap, int mode){
        HashMap retConnsMap = new HashMap();
        try {
            Iterator it = srcConnsMap.keySet().iterator();
            String connsKey = null;
            HashMap conn = new HashMap();
            String connKey = null;
            String connValue = null;
            while(it.hasNext()){
                connsKey = it.next().toString();
                conn = (HashMap)srcConnsMap.get(connsKey);
                Iterator itt = conn.keySet().iterator();
                HashMap retConnMap = new HashMap();
                while(itt.hasNext()){
                    connKey = itt.next().toString();
                    connValue = conn.get(connKey).toString();
                    switch(mode){
                        case CommonConst.ENCRYPT:
                            connKey = jc.encrypt(connKey);
                            connValue = jc.encrypt(connValue);
                            break;
                        case CommonConst.DECRYPT:
                            connKey = jc.decrypt(connKey);
                            connValue = jc.decrypt(connValue);
                            break;
                        default:
                            break;
                    }
                    retConnMap.put(connKey, connValue);
                }
                retConnsMap.put(connsKey, retConnMap);
            }            
        } catch (Exception ex) {
            ed.viewExceptionDialog(ex);
        }
        return retConnsMap;
    }
    
//    private HashMap cipherCurrConnInfo(HashMap plainHashMap, int mode){
//        HashMap encCurrConnMap = new HashMap();
//        try {
//            Iterator it = plainHashMap.keySet().iterator();
//            String connKey = null;
//            String connValue = null;
//            while(it.hasNext()){
//                connKey = it.next().toString();
//                connValue = plainHashMap.get(connKey).toString();
//                switch(mode){
//                    case CommonConst.ENCRYPT:
//                        connKey = jc.encrypt(connKey);
//                        connValue = jc.encrypt(connValue);
//                        break;
//                    case CommonConst.DECRYPT:
//                        connKey = jc.decrypt(connKey);
//                        connValue = jc.decrypt(connValue);
//                        break;
//                    default:
//                        break;
//                }
//                encCurrConnMap.put(connKey, connValue);
//            }        
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//        return encCurrConnMap;
//    }
    
//    private void writeCurrConnInfo() {
//        try {
//            encCurrConn = cipherCurrConnInfo(mCurrConn,CommonConst.ENCRYPT);
//            FileHandler.writeSerFile(CommonConst.CURR_CONNECT_INFO_SER_FILE, encCurrConn, null);
//        } catch (JayException ex) {
//            Logger.getLogger(ConnectDialogBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    
    private void setInitConnsInfo(){
        this.DBNameField.setText(null);
        this.HostIPField.setText(null);
        this.PasswordField.setText(null);
        this.PortField.setText(null);
        this.UserField.setText(null);
        this.DatabaseTypeCombo.setSelectedIndex(0);
        this.LabelField.setText(null);
        this.TestButton.setEnabled(true);
        this.mCurrConn.clear();        
    }
    
    private void refreshConnsInfo(){
        try {
            encConns = cipherConnsInfo(mConnections,CommonConst.ENCRYPT); 
            FileHandler.writeSerFile(CommonConst.CONNECT_INFO_SER_FILE, this.encConns, System.getProperty(CommonConst.USER_DIR_PROP_KEY)+File.separator+CommonConst.LIB_DIR);
//            System.out.println(System.getProperty(CommonConst.USER_DIR_PROP_KEY)+File.separator+CommonConst.LIB_DIR);            
        } catch (JayException ex) {
            ed.viewExceptionDialog(ex);
        }
        this.setConnsTable();
    }
    
    private void sortTable(){
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(this.jTable1.getModel());
        this.jTable1.setRowSorter(sorter);
    }
    
    private void saveConnsInfo(){
        try {
            this.makeCurrConn();            
            if(this.mModfFlag){
//                System.out.println(this.mConnections.get(this.mSelectedKey));
                this.mConnections.remove(mSelectedKey);
//                System.out.println(this.mConnections.get(this.mSelectedKey));
            }
            this.mConnections.put(CommonUtil.makeUniqueID(), mCurrConn);                 
            refreshConnsInfo();
        } catch (Exception ex) {
            ed.viewExceptionDialog(ex);
        }
    }
    
    private HashMap readConnsInfo(){
        HashMap ret = null;
        try {
            ret = new HashMap();
            ret = (HashMap)FileHandler.readSerFile(System.getProperty(CommonConst.USER_DIR_PROP_KEY)+File.separator+CommonConst.LIB_DIR+File.separator+CommonConst.CONNECT_INFO_SER_FILE);
            if(ret != null) ret = cipherConnsInfo(ret, CommonConst.DECRYPT);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }
    
    private Vector toVector(HashMap aConnInfoMap){
        Vector ret = new Vector();
        
        Iterator dataIt = aConnInfoMap.keySet().iterator();
    	HashMap currentRow = null;
        String currentRowKey = null;
        String colValue = null;
    	while(dataIt.hasNext()){
            currentRowKey = (String)dataIt.next();
            currentRow = (HashMap)aConnInfoMap.get(currentRowKey);
            Vector row = new Vector();
            row.addElement(currentRowKey);
            for(int i=1;i<CommonConst.CONNECT_CONFIG_COLUMN.length;i++){
                colValue = (String)currentRow.get(CommonConst.CONNECT_CONFIG_COLUMN[i]);
                row.addElement(colValue);
            }
            ret.addElement(row);
    	}
        
        return ret;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton ConnectButton;
    private javax.swing.JPanel DBConnectionInfoPannel;
    private javax.swing.JTextField DBNameField;
    private javax.swing.JLabel DBNameLabel;
    private javax.swing.JComboBox DatabaseTypeCombo;
    private javax.swing.JLabel DatabaseTypeLabel;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JButton Export;
    private javax.swing.JTextField HostIPField;
    private javax.swing.JLabel HostIPLabel;
    private javax.swing.JLabel InformLabel;
    private javax.swing.JTextField LabelField;
    private javax.swing.JButton NewButton;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JTextField PortField;
    private javax.swing.JLabel PortLabel;
    private javax.swing.JButton SaveButton;
    private javax.swing.JButton TestButton;
    private javax.swing.JTextField UserField;
    private javax.swing.JLabel UserLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private boolean checkJDBCConnection() throws Exception {
        boolean ret = false;
        this.makeCurrConn();
        DBInfo di = new DBInfo(
                                (String)mCurrConn.get(CommonConst.SID_STRING),
                                (String)mCurrConn.get(CommonConst.HOST_STRING),
                                Integer.parseInt((String)mCurrConn.get(CommonConst.PORT_STRING)),
                                (String)mCurrConn.get(CommonConst.DB_TYPE_STRING),
                                (String)mCurrConn.get(CommonConst.USER_STRING),
                                (String)mCurrConn.get(CommonConst.PASSWORD_STRING)
                );
        DatabaseConnection dc = new DatabaseConnection(di);
        dc.connect();
//        System.out.println(dc.isConnectionAlive());
        ret = dc.isConnectionAlive();
        dc.disconnect();
        return ret;
    }
}